package com.skt.common_hr.overtime.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.overtime.model.HolidayProxy;
	import com.skt.common_hr.overtime.model.MemberOtProxy;
	import com.skt.common_hr.overtime.model.vo.MemberOtVO;
	import com.skt.common_hr.overtime.view.components.MemberOtView;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class MemberOtMediator extends CommonMediator
	{
		public static const NAME:String = "MemberOtMediator"; 
		
		public static const SAVE_MEMBER_OT_COMPLETE:String = "saveMemberOtComplete";
		public static const CLOSE_MEMBER_OT_VIEW:String = "closeMemberOtView";
		
		//private var jichakList:Array = ["716", "730", "718", "713", "714", "720", "715" ];
		private var jichakList:Array = ["712", "713", "714", "715", "716"/*,"717"*/, "718"/*, "719"*/, "720", "721"/*, "722"*/,
										"730", "741", "803", "805"/*, "807"*/, "831", "833", "835", "837", "839", "841"/*, "843", "845"*/];
		private var bossCodes:Array = ["712", "713", "714", "715", "718", "720", "721", "730", "741", "805", "831", "833", "835", "841"];
		private var holiday:Boolean = false;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function MemberOtMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			setEventListener();
 		}


		private function setEventListener():void
		{
 			// 각 버튼들의 Event Handler를 설정하고
 			view.addEventListener(MemberOtView.CHANGED_DATE, onChangedDate);
 			view.addEventListener(MemberOtView.CHANGED_DEPT, onChangedDept);
 			view.addEventListener(MemberOtView.CONFIRM, onClickConfirm);
 			view.addEventListener(MemberOtView.CANCEL, onClickCancel);
		}


 		public function exec(selectedDate:Date, selectedDept:String, deptOt:Number, deptList:ArrayCollection):void
 		{
 			bActivated = true;
 			view.otList = new ArrayCollection();
 			view.selectedDate = selectedDate;
 			view.deptList = deptList;
 			view.deptOT = deptOt;
			holiday = checkHoliday(selectedDate);
			 			
 			if(StringUtil.NVL(selectedDept) == "") {
 				loadList();
 			}
 			else { 
 				setDeptIndex(selectedDept);
 			}
 		}
 		
		private function loadList():void 
		{
			var proxy:MemberOtProxy = facade.retrieveProxy(MemberOtProxy.NAME) as MemberOtProxy;
			proxy.load(view.selectedDate, view.selectedDept);
		}
		
		private function onChangedDate(e:Event):void
		{
			
		}
		
		private function onChangedDept(e:Event):void
		{
			loadList();
		}
		
		private function onClickConfirm(e:Event):void
		{
			var proxy:MemberOtProxy = facade.retrieveProxy(MemberOtProxy.NAME) as MemberOtProxy;
			proxy.save(view.selectedDate, view.selectedDept, getOtList());
		}
		
		private function onClickCancel(e:Event):void
		{
			closeView();
		}
		

		private function setDeptIndex(dept:String):void
		{
			for(var i:int=0; i<view.deptList.length; i++) {
				if(view.deptList.getItemAt(i).data == dept) {
					view.cbDept.selectedIndex = i;
					view.selectedDept = dept;
					loadList();
					return;
				}
			}
		}


 		private function getOtList():Array 
 		{
 			var arr:Array = new Array;
 			
 			for(var i:int=0; i<view.otList.length; i++) {
 				var vo:Object = new Object();
 				vo.PERNR = view.otList.getItemAt(i).PERNR;
 				vo.ENAME = view.otList.getItemAt(i).ENAME;
 				vo.OTWOK = view.otList.getItemAt(i).OTWOK.toString();
 				vo.TEXT1 = view.otList.getItemAt(i).TEXT1;
 				arr.push(vo);
 			}
 			return arr;
 		}


		private function checkHoliday(d:Date):Boolean
		{
			var proxy:HolidayProxy = facade.retrieveProxy(HolidayProxy.NAME) as HolidayProxy;
			if(d == null) {
				return false;
			}
			if(d.day==6 || d.day==0) {
				return true;
			}
			else {
				for each(var h:Date in proxy.list) {
					if(h.fullYear==d.fullYear && h.month==d.month && h.date==d.date) {
						return true;
					}
				}				
				return false;
			}
			
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					MemberOtProxy.LOAD_MEMBER_OT_COMPLETE,
					MemberOtProxy.LOAD_MEMBER_OT_FAILED,
					MemberOtProxy.SAVE_MEMBER_OT_COMPLETE,
					MemberOtProxy.SAVE_MEMBER_OT_FAILED
					];
		}
		
		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			if(bActivated) {
				switch(note.getName()) 
				{
					case MemberOtProxy.LOAD_MEMBER_OT_COMPLETE:
						view.otList = procHoliday((note.getBody() as MemberOtProxy).list);
						break;
					case MemberOtProxy.LOAD_MEMBER_OT_FAILED:
						break;
					case MemberOtProxy.SAVE_MEMBER_OT_COMPLETE:
						//facade.sendNotification(SAVE_OT_COMPLETE, this);
						closeView();
						break;
					case MemberOtProxy.SAVE_MEMBER_OT_FAILED:
						//CommonAlert.showMessage("", CommonProxy(MemberOtProxy).retText);
						//2014.0212 김선우과장 요청 
						CommonAlert.showMessage("", "("+note.getBody().retCode+")"+note.getBody().retText);
						break;
				}
			}
		}

		private function procHoliday(src:ArrayCollection):ArrayCollection
		{
			var i:int;
			if(!holiday) {
				for (i=0; i<src.length; i++) {
					if(jichakList.indexOf((src.getItemAt(i) as MemberOtVO).JIKCHAK) >=0) {
						src.removeItemAt(i--);
					}
				}
			}
			else {
				for (i=0; i<src.length; i++) {
					if(bossCodes.indexOf((src.getItemAt(i) as MemberOtVO).JIKCHAK) >=0) {	// CS실장은 제외
						var rec:* = src.removeItemAt(i);
						src.addItemAt(rec, 0);
					}
				}
			}
			return src
		}

 		private function closeView(cancel:Boolean=false):void
		{
			bActivated = false;
			facade.sendNotification(CLOSE_MEMBER_OT_VIEW, this);
			PopUpManager.removePopUp(view);
		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        public function get view():MemberOtView
        {
            return viewComponent as MemberOtView;
        }
	}
}